﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AssetManagementSystem.Model {
    public class Loan {
        int id;
        DateTime startDate;
        DateTime dueDate;
        string studentId;
        Dictionary<string, DateTime> assets;

        public Loan(DateTime startDate, DateTime dueDate, string studentId, Dictionary<string, DateTime> assets)
            : this(-1, startDate, dueDate, studentId, assets) {
        }

        public Loan(int id, DateTime startDate, DateTime dueDate, string studentId, Dictionary<string, DateTime> assets) {
            this.id = id;
            this.startDate = startDate;
            this.dueDate = dueDate;
            this.studentId = studentId;
            this.assets = assets;
        }

        //returns true if a return date has been set.
        public bool IsReturned() {
            return !assets.ContainsValue(DateTime.MinValue);
        }

        //returns true if the current date is past the due date for this loan.
        public bool IsOverdue() {
            return (!IsReturned() && this.dueDate < DateTime.Now);
        }

        //return the number of days left until the loan should be returned
        // if overdue or returned, returns 0.
        public int GetDaysLeft() {
            if (!IsReturned() && !IsOverdue()) {
                TimeSpan span = this.dueDate.Subtract(DateTime.Now);
                return (int)span.TotalDays;
            } else if (IsOverdue()) {
                return -1;
            }
            return 0;
        }

        public override string ToString() {
            string returnString = "Loan:" + id.ToString()
                 + ", Start:" + startDate.ToString()
                 + ", Due:" + dueDate.ToString();
            if (IsReturned()) {
                returnString += ", Returned";
            } else if (IsOverdue()) {
                returnString += ", Overdue";
            }
            returnString += ", Student:" + studentId.ToString();

            return returnString;
        }

        // Getters and Setters ===============================================
        public int Id {
            get { return id; }
            //set { id = value; }
        }
        public DateTime StartDate {
            get { return startDate; }
            set { startDate = value; }
        }
        public DateTime DueDate {
            get { return dueDate; }
            set { dueDate = value; }
        }
        public string StudentId {
            get { return studentId; }
            set { studentId = value; }
        }
        public Dictionary<string, DateTime> Assets {
            get { return assets; }
            set { assets = value; }
        }
        // END Getters and Setters ===========================================
    }
}
